package com.shk.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.shk.constant.MessageConstant;
import com.shk.dao.MemberDao;
import com.shk.dao.OrderDao;
import com.shk.dao.OrderSettingDao;
import com.shk.entity.Result;
import com.shk.pojo.Member;
import com.shk.pojo.Order;
import com.shk.pojo.OrderSetting;
import com.shk.service.OrderService;
import com.shk.service.OrderSettingService;
import com.shk.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
/*前端提交预约*/
    @Override
    public Result submit(Map map) throws Exception {
      /* 1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
        5、预约成功，更新当日的已预约人数*/
        /*取出orderDate*/
        String date = (String) map.get("orderDate");
        /*转换为数据库所需要的Date类型*/
        Date orderDate = DateUtils.parseString2Date(date);
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByDate(orderDate);
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if(orderSetting.getReservations()>orderSetting.getNumber()){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        /*通过电话号码去member表得到用户id 然后通过用户id  预约日期 和 套餐id去order表中查  如果有数据说明该用户正在重复预约*/
        String setmealId = (String) map.get("setmealId");

        String phoneNumber = (String) map.get("telephone");

        Member member = memberDao.findMemberByTelephone(phoneNumber);

            if(member != null){
                Order order =orderDao.findByMemberIdAndOrderDateAndSetmealId(member.getId(),orderDate,Integer.parseInt(setmealId));
                if(order !=null){
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }else {
                member = new Member();
                member.setName((String) map.get("name"));
                member.setSex((String) map.get("sex"));
                member.setPhoneNumber(phoneNumber);
                member.setIdCard((String) map.get("idCard"));
                member.setRegTime(new Date());
                memberDao.add(member);
            }

                /*如果该用户是会员 直接完成预约*/
        Order order = new Order();

        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt(setmealId));
        orderDao.add(order);

        /*在预约设置增添预约人数*/
        orderSetting.setReservations(orderSetting.getReservations()+1);/*预约人数+1*/
        /*通过预约日期去修改已预约的人数*/
        orderSettingDao.updateReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

    }

    @Override
    public Map findById(Integer id) {
      Map map= orderDao.findById(id);
        return map;
    }

}
